﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using HZH_Controls;
using HZH_Controls.Controls;

namespace Lamex.mSchedule.UI
{
    public partial class frmSCH_Position_Search : frmBaseChild
    {
        public frmSCH_Position_Search()
        {
            InitializeComponent();
        }

        private void frmSCH_Position_Search_Load(object sender, EventArgs e)
        {
            gridView2.OptionsView.ShowIndicator = false;
        }

        private void frmSCH_Position_Search_Resize(object sender, EventArgs e)
        {
            panelRight.Width = this.Width - panelLeft.Width;
            panelLft_Bottom.Height = panelLeft.Height - panelLft_Top.Height;
            panelRgt_Bottom.Height = panelRight.Height - panelRgt_Top.Height;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string strQuery = "";

            if (radioManufacture_No.Checked == true)
            {
                strQuery = " WHERE MO号码 = ";
            }
            else if (radioFollow_No.Checked == true)
            {
                strQuery = " WHERE 跟进表号 = ";
            }
            else if (radioBarcode.Checked == true)
            {
                strQuery = " WHERE 条码编号 = ";
            }

            strQuery = strQuery + "'"  + textBox1.Text.ToString() + "'";
            setGridData_Details(strQuery);

            setGridData_OnlyHeader();

            refreshSteps(strQuery);
        }

        private void setGridData_Details(string strQuery)
        {
            string StrSql = @"SELECT MO号码,跟进表号,物料号,图号,描述,条码编号
                            FROM OP_Follow_Process_Flow" + strQuery;

            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(StrSql);

            gridControl1.DataSource = dt;
            //setGridViewColumnAutoWidth(gridView1);
            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.Columns[0].BestFit();
            gridView1.Columns[1].BestFit();
            //gridView3.BestFitColumns();
            //setGridViewColumnAutoWidth();
        }

        private void setGridData_OnlyHeader()
        {
            string str = "";
            string StrSql = @"select * from syscolumns where id=object_id('OP_Follow_Process_Flow')";

            DataTable dt1 = Lamex.Base.BaseBuildSql.GetDataTableBySql(StrSql);

            DataTable dt2 = new DataTable();
            DataRow dr = dt2.NewRow();

            for (int i = 9; i < dt1.Rows.Count; i++) 
            {
                str = dt1.Rows[i][0].ToString();
                dt2.Columns.Add(new DataColumn(str.Substring(0, 3), typeof(string)));
                dr[i - 9] = str.Substring(str.Length - (str.Length-4));
            }

            dt2.Rows.Add(dr);
            gridControl2.DataSource = dt2;
        }

        private void refreshSteps(string strQuery)
        {
            int n = 0;

            string StrSql = @"SELECT MO号码,跟进表号,物料号,图号,描述,条码编号,工序数量,当前位置,当前工序
                            FROM OP_Follow_Process_Flow" + strQuery;

            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(StrSql);

            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i < dt.Rows.Count; i++) 
                {
                    n = n + 40;
                    UCStep ucStep = new UCStep();
                    ucStep.Location = new Point(12,n);
                    ucStep.Size = new System.Drawing.Size(this.panelRgt_Bottom.Width.ToInt()-20,24);
                    //加载步骤信息
                    string[] arrayStr = new string[dt.Rows[i]["工序数量"].ToInt()];
                    for (int j = 0; j < arrayStr.Length; j++)
                    {
                        arrayStr[i] = "" ;
                    }

                    ucStep.Steps = arrayStr;
                    ucStep.StepIndex = dt.Rows[i]["当前位置"].ToInt();
                    this.panelRgt_Bottom.Controls.Add(ucStep);
                }
            }
        }
    }
}